/*
*作者---杨彬
*
*/
<template>
    <div>
     <header class="detail-head">
       <span v-text="sData.myOrder.orderNumberCn+' '"></span>
       <span v-text="defaultData.sn"></span>
       <span class="order-type com-div-middle-ab"
             v-text="sData.myOrder.type[defaultData.status].name"></span>
     </header>
      <section>
        <div class="address-warp">
          <p class="name-tel-warp">
            <span class="name" v-text="defaultData.name"></span>
            <span class="tel" v-text="defaultData.tel"></span>
          </p>
          <div class="address-img-warp">
            <img class="com-div-middle-ab" src="../../assets/address/location.png">
            <p class="address" v-text="defaultData.address"></p>
          </div>
        </div>
        <div class="content-warp">
        <div class="content-item-warp" v-for="item in productData">
            <img :src="item.thumbnail">
          <div class="content-item" :class="{imgAccountMore:defaultData.img.length>1}">
            <p class="title" v-text="item.name"></p>
            <p class="detail" v-text="item.intro"></p>
          </div>
          <p class="money"
             v-text="sData.myOrder.detail.rmb+item.price"></p>
          <p class="number"
             v-text="sData.myOrder.detail.textOther[0]+item.quantity"></p>
        </div>
        </div>
        <div class="order-time-warp">
          <div v-for="fitem in sData.myOrder.detail.textTime"
               class="order-time-item">
            <span class="navText" v-text="fitem.name"></span>
            <span class="navValue com-div-middle-ab" v-text="fitem.key?fitem.key:'无'"></span>
          </div>
        </div>
        <div class="cost-sum-warp">
            <div class="cost-sum-content">
              <p v-for="(gitem,gindex) in sData.myOrder.detail.textCost">
                <span class="cost-title" v-text="gitem.name"></span>
                <span class="cost-sum com-div-middle-ab" v-text="defaultData.number+sData.myOrder.detail.textOther[1]" v-if="gindex==0"></span>
                <span class="cost-number">
                   <span class="symbol" v-text="sData.myOrder.detail.costSymbol(gindex)"></span>
                   <span class="val" v-text="gitem.key"></span>
                </span>
              </p>
            </div>
            <div class="cost-sum-foot">
              <span class="navText"
                    v-text="defaultData.status==0?sData.myOrder.realPayCn[0]:sData.myOrder.realPayCn[1]"></span>
              <span class="navValue">
                <span class="symbol" v-text="sData.myOrder.detail.rmb"></span>
                <span class="val" v-text="Number(defaultData.amountPaid).toFixed(2)"></span>
              </span>
            </div>
        </div>
      </section>
      <footer class="detail-foot">
        <div class="div-place"></div>
        <div class="item-operation" v-if="sData.myOrder.type[defaultData.status].url.length>0">
          <div class="surplus-time-warp com-div-middle-ab" v-if="defaultData.status==0">
            <p class="surplus-time-cn" v-text="sData.myOrder.detail.textOther[2]"></p>
            <p class="surplus-time" v-text="defaultData.leftTime"></p>
          </div>
          <button type="button" v-text="sitem.text" @click="itemOperation(defaultData,sitem.val)"
                  v-for="sitem in sData.myOrder.type[defaultData.status].url"></button>
        </div>
      </footer>
      <div class="pop" v-show='cancelOrder'>
       <div>
           <p class='tit'>确认取消此订单吗?</p>
           <p class='btn'>
               <span class='cancel' @click = 'cancel'>取消</span>
               <span class='submit' @click = 'sure'>确认</span>
           </p>
       </div>
    </div>
    </div>
</template>
<script type="text/javascript">
  export  default {
    props:['sData'],
    data(){
      return{
        cancelOrder:false,//取消弹框
        defaultData:
          { sendTime:"2017-1-11 10:00~11:00",
            orderTime:"2017-01-11 10:00",
            remark:"请尽快配送",
            number:10,
            status:this.$route.params.id,
            sn:"232321111222",
            leftTime:'00:00',
            money:"100",title:'草莓千层水果蛋糕',
            detail:'4寸/3套餐具/生日帽/巧克力牌:happy/数字蜡烛:20',
            img:[1], freight:20,point:100,coupon:20,
            address:'四川省成都市双流县华阳镇街道龙灯山路一段名著司南1栋1单元011',tel:'18202830089',name:'张虹',
            amountPaid:''
          }
        ,productData:[],
        time:20,
        currentOrder:'',
        lastTime:0,
        timer:''
      }
    },
    created () {
      this.getAjax(this.sData.url.findOrderDetail,{ orderId : m$.sessionStores.get("orderData").id},(res)=>{
        //取消成功再次获取数据
        if(res && res.message=='OK'){
          console.log(res.data);
          console.log('创建时间');
          console.log(res.data.createDate);
          console.log(new Date(res.data.createDate.replace(/\-/g,'/')).getTime());
          console.log(new Date().getTime());
          //付款剩余时间
          var othis = this;
          var nowTime = res.data.nowTime;
          this.timer = setInterval(function(){
            othis.lastTime = 1800-(Number(nowTime) - Number(new Date(res.data.createDate.replace(/\-/g,'/')).getTime()))/1000;
            if(othis.lastTime<0){
              othis.defaultData.leftTime = '已超时';
            }else{
              //让分钟为两位
              if(parseInt(othis.lastTime/60) < 10){

                var minutes = '0'+'' +(parseInt(othis.lastTime/60)+"") ;
                console.log(minutes)
              }else{
                var minutes = parseInt(othis.lastTime/60)+'';
              }
              //让秒为两位
              if(parseInt(othis.lastTime%60)<10){
                var second = '0' + ''+parseInt(othis.lastTime%60);
              }else{
                var second = parseInt(othis.lastTime%60)+'';
              }
              othis.defaultData.leftTime = ''+minutes + ':' + second;
            }
            nowTime = nowTime + 1000;
          },1000)
          this.productData                          = res.data.orderItemBeans;
          this.defaultData.sn                       = res.data.sn;

          this.sData.myOrder.detail.textTime[0].key = res.data.sendTime;
          this.sData.myOrder.detail.textTime[1].key = res.data.intro;
          this.sData.myOrder.detail.textTime[2].key = res.data.createDate;
          this.sData.myOrder.detail.textTime[3].key = '微信';//支付方式
          this.sData.myOrder.detail.textTime[4].key = res.data.snOut//运单号
          this.sData.myOrder.detail.textTime[5].key = res.data.shippingMethod;

          this.defaultData.amountPaid               = res.data.amountPaid;//商品优惠后总价
          this.defaultData.number                   = res.data.totalQuality;//商品总价
          this.sData.myOrder.detail.textCost[0].key = res.data.totalAmount.toFixed(2);//商品数量
          this.sData.myOrder.detail.textCost[1].key = Number(res.data.fee).toFixed(2)?Number(res.data.fee).toFixed(2):'0.00';//额外餐具费用
          this.sData.myOrder.detail.textCost[2].key = Number(res.data.freight).toFixed(2);//运费
          this.sData.myOrder.detail.textCost[3].key = Number(res.data.shopingPoint/100).toFixed(2);//积分券
          if(res.data.priceExpression>(res.data.totalAmount+res.data.fee)){
            this.sData.myOrder.detail.textCost[4].key = Number(res.data.totalAmount+res.data.fee).toFixed(2);//优惠券
          }else{
            this.sData.myOrder.detail.textCost[4].key = Number(res.data.priceExpression).toFixed(2);//优惠券
          }
          

          this.getAjax(this.sData.url.findReceiver,{ userId : this.sData.userId,receiveId:res.data.receiveId},(res)=>{
            //取消成功再次获取数据
            if(res && res.message=='OK'){
              this.defaultData.name    = res.data[0].consignee
              this.defaultData.tel     = res.data[0].phone
              this.defaultData.address = res.data[0].provinceName+res.data[0].cityName+res.data[0].districtName+res.data[0].detailAddress
            }else {}
          })

        }else {}
      })

    },
    methods : {
      cancel(){
        this.cancelOrder = false;
      },
      sure (){
          this.postAjax(this.sData.url.cancelOrderUrl,{ id:m$.sessionStores.get("orderData").id},(res)=>{
            //取消成功再次获取数据
            if(res && res.message=='OK'){
              window.location.href=(`#/orderList/${this.currentOrder.status}`);
            }else {}
          })
          this.cancelOrder = false;
      },
      itemOperation (defaultData,operaionId) {
          console.log(defaultData.status)
          if(operaionId==1){//立即支付
            if(Number(this.defaultData.amountPaid)==0){
              this.postAjax(this.sData.url.payWeixin,{ orderId : m$.sessionStores.get("orderData").id,amount : 0},(res)=>{
                if(res && res.message == 'OK' ){
                  m$.template({
                    val: '支付成功啦',
                    time: 1500
                  });
                  window.location.href=(`#/orderList/-1`);

                }else {
                  m$.template({
                    val: '服务器开小差啦,请重试',
                    time: 1500
                  })
                }
              })
            }else{
              window.location.href=(`#/goPay/${m$.sessionStores.get("orderData").id}`);
            }
            
          }else if(operaionId==0){//取消订单
            this.cancelOrder = true;
            this.currentOrder = defaultData;
          }else if(operaionId==2){//查看物流

          }else if(operaionId==3){//确认收货
            console.log('确认收货');
            this.postAjax(this.sData.url.sureReceive,{orderId:m$.sessionStores.get("orderData").id},(res)=>{
                console.log(res);
                if(res && res.status==200){
                  //重新获取数据
                  m$.template({
                    val:'收货成功',
                    time:1200
                  })
                  //再次获得数据
                  var othis = this;
                  setTimeout(function(){
                    window.history.back(-1);
                  },1000)
                }
            })
          }else if(operaionId==4){//立即评价
            //window.location.href=(`#/evaluate/${m$.sessionStores.get("orderData").id}`);
            this.$router.replace('/evaluate/'+m$.sessionStores.get("orderData").id);
          }else if(operaionId==5){//再次购买
            
          }
      }
    },
    updated(){
    },
    beforeDestroy(){
      // m$.sessionStores.remove("orderData");
      clearInterval(this.timer);
    }
  }
</script>
<style scoped lang="less">
    @import "orderDetail";
</style>
